package com.jiang.demo.controller;

import com.jiang.demo.common.MSG;
import com.jiang.demo.entity.UserEntity;
import com.jiang.demo.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "user接口测试")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/")
    public MSG<Long> save(@RequestBody UserEntity user){
        Long save = userService.save(user);
        return MSG.SUCCESS(save);
    }

    @DeleteMapping("/{id}")
    public MSG<Long> delete(@PathVariable Long id){
        Long save = userService.delete(id);
        return MSG.SUCCESS(save);
    }

    @PutMapping("/")
    public MSG<Long> update(@RequestBody UserEntity user){
        Long save = userService.update(user);
        return MSG.SUCCESS(save);
    }

    @GetMapping("/list")
    public MSG<List<UserEntity>> getUserList(){
        List<UserEntity> userList = userService.getUserList();
        return MSG.SUCCESS(userList);
    }

    @GetMapping("page")
    public MSG<Page<UserEntity>> getUserPage(){
        Page<UserEntity> userPage = userService.getUserPage(0, 5);
        return MSG.SUCCESS(userPage);
    }
}
